﻿using System;
using System.Collections;
using System.Data;
using System.IO;

namespace BdiSyndicatonWarmup
{
    public class BdiFileRead
    {
        public static ArrayList GetCsvFileName(string folderPath)
        {
            ArrayList list = new ArrayList();

            DirectoryInfo folder = new DirectoryInfo(folderPath);
            if (folder.Exists)
            {
                foreach (FileInfo file in folder.GetFiles("*.Csv"))
                {
                    list.Add(file.Name);
                }
            }

            return list;
        }

        public static string GetQueryFileName()
        {
            DirectoryInfo folder = new DirectoryInfo("QueryFile");
            if (folder.Exists)
            {
                foreach (FileInfo file in folder.GetFiles())
                {
                    return file.FullName;
                }
            }

            return string.Empty;
        }

        public static ArrayList GetFileName(string filePath)
        {
            ArrayList list = new ArrayList();

            using(FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            using (StreamReader sr = new StreamReader(fs))
            {
                sr.BaseStream.Seek(0, SeekOrigin.Begin);

                string str = sr.ReadLine();
                while (str != null)
                {
                    list.Add(str);
                    str = sr.ReadLine();
                }

                sr.Close();
                fs.Close();
            }
            return list;
        }

        public static ArrayList ReadMachineNameFromCsvFile(string filePath)
        {
            ArrayList list = new ArrayList();
            try
            {
                using (DataTable dt = ReadCsvFile(filePath))
                {
                    if (dt == null)
                    {
                        return null;
                    }
                    if (dt.Columns.Count <= 3)
                    {
                        return null;
                    }

                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr[2].ToString().ToUpper() == "BDISYN")
                        {
                            list.Add(dr[0]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private static DataTable ReadCsvFile(string filePath)
        {
            StreamReader objReader = new StreamReader(filePath, System.Text.Encoding.Default);
            string strline = "";
            string[] aryline;
            bool blnFlag = true;
            ArrayList arrText = new ArrayList();
            DataTable dt = new DataTable();
            DataColumn dc;
            DataRow dr;

            try
            {
                while ((strline = objReader.ReadLine()) != null)
                {
                    if (string.IsNullOrEmpty(strline.Trim()))
                    {
                        continue;
                    }

                    if (strline.StartsWith("#"))
                    {
                        continue;
                    }

                    aryline = strline.Split(',');

                    if (blnFlag)
                    {
                        blnFlag = false;
                        for (int i = 0; i < aryline.Length; i++)
                        {
                            dc = new DataColumn((i + 1).ToString());
                            dt.Columns.Add(dc);
                        }
                    }
                    dr = dt.NewRow();
                    for (int i = 0; i < aryline.Length; i++)
                    {
                        dr[i] = aryline[i];
                    }
                    dt.Rows.Add(dr);
                }
                objReader.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }
    }
}
